<?php
namespace app\wx\controller;
use think\Controller;
//use think\Request;
use think\config;

// use think\Db;
class Upload extends Controller
{
   public function  http_curl($url,$data=null){

		//1.初始化，创建一个新cURL资源
		 
		$ch = curl_init();
		 
		//2.设置URL和相应的选项
		 
		curl_setopt($ch, CURLOPT_URL, $url);
		 
        //curl_setopt($ch, CURLOPT_HEADER, 0);
        //如果data 不为空，则是要上传文件
		 if(!empty($data)){
		 	curl_setopt($ch,CURLOPT_POST,1);
		 	
		 	curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		 }


		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//禁止curl资源直接输出
		//3.抓取URL并把它传递给浏览器
		 
		$opt=curl_exec($ch);
		 	

		//4.关闭cURL资源，并且释放系统资源
		 
		curl_close($ch);
		return $opt;
	}
    
	public function  get_token(){
		$appid="wx4f295a95a89349e2";
		$secret="e330d58176e6be80780634f3d14deebc";

		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
       //用http_curl发给微信 微信的反回值计入$json
		$json=$this->http_curl($url);
		$result=json_decode($json);
        // dump($result->access_token);
		return $result->access_token;
	}


	/***
	 * 上传菜单 只能在测试号 使用
	 */
	public function  up(){
		$token = $this->get_token();
	//	dump($token);
	//	exit;
		$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$token}";
		$data = '{
			"button": [
				{
					"type": "click", 
					"name": "哈12", 
					"key": "V1001_TODAY_MUSIC"
				}, 
				{
					"name": "影视", 
					"sub_button": [
						{
							"type": "view", 
							"name": "搜索", 
							"url": "http://www.soso.com/"
						}, 
						{
							"type": "click", 
							"name": "赞一下我们", 
							"key": "V1001_GOOD"
						}
					]
				}
			]
		}';
	// result 是反回的参数
	$result= $this->http_curl($url,$data);

	dump($result);

	}


	



}

